package programm;

import jniGlue.Librarylinker;
import soundChaser.*;

/**
 * This Threadclass executes the other threads, so that the GUI class can change
 * its optic.
 * 
 * @author jm
 * 
 */
public class C_audioPlayer extends Thread
{
	private Librarylinker fx5;

	V_audioPlayer view;

	// Audio Variables
	private String mp3ort;
	AudioPlayer player;
	AudioAnalyzer audioAna;
	double[][] wavearray;
	long waveBitrate;

	public C_audioPlayer(V_audioPlayer tempview, final Librarylinker currentos)
	{
		this.fx5 = currentos;

		this.view = tempview;
	}

	public void run()
	{
		view.lblWaitingSelected.setVisible(true);

		mp3ort = (new OwnFileChooser(".mp3", "Bitte eine MP3 Datei waehlen!")
				.getPath());

		if (mp3ort != null)
		{
			view.lblWaitingSelected.setVisible(false);

			view.chckbxFileselected.setSelected(true);
			view.chckbxWavecreated.setSelected(false);
			view.chckbxWaveread.setSelected(false);
			view.chckbxComputation.setSelected(false);

			view.lblWaitingCreated.setVisible(true);

			WaveLesen wlObject = new WaveLesen(mp3ort);

			// get the temp folder, for saving the wave file
			String tmpDir = System.getProperty("java.io.tmpdir");
			tmpDir = tmpDir.replaceAll("\\\\", "/");
			// if there is no file seperator at the end set it
			if (tmpDir.lastIndexOf("/") != tmpDir.length() - 1)
				tmpDir += "/";

			// replace the .mp3 ending with .wav
			String filename = mp3ort.substring(mp3ort.lastIndexOf("/") + 1);
			filename = filename.substring(0, filename.lastIndexOf("."))
					+ ".wav";

			// save the wave file in the temp folder
			String waveort;
			waveort = wlObject.convert(tmpDir + filename);
			view.chckbxWavecreated.setSelected(true);
			view.lblWaitingCreated.setVisible(false);

			// get the wavearray from the saved wave file
			view.lblWaitingRead.setVisible(true);
			wavearray = wlObject.wave2array(waveort);
			waveBitrate = wlObject.getSampleRate();
			view.chckbxWaveread.setSelected(true);
			view.lblWaitingRead.setVisible(false);

			// do the computation
			view.lblWaitingComputation.setVisible(true);
			audioAna = new AudioAnalyzer(fx5);

			try
			{
				audioAna.initialize(wavearray, waveBitrate);
			} catch (InterruptedException e)
			{
				e.printStackTrace();
			}

			view.chckbxComputation.setSelected(true);
			view.lblWaitingComputation.setVisible(false);

			// when all threads are safe

			view.btnStartplayback.setEnabled(true);
			view.btnStopplayback.setEnabled(false);
		} else
		{
			view.lblWaitingSelected.setVisible(false);
		}
		view.btnSelectmp3.setEnabled(true);
	}

	public void playerstart()
	{
		view.btnStopplayback.setEnabled(true);
		view.btnStartplayback.setEnabled(true);
		view.btnSelectmp3.setEnabled(false);
		
		if (player == null) // player is initialized?
		{
			player = new AudioPlayer(mp3ort);
		}

		if (!player.isAlive()) // when the player already runs, do nothing
		{
			view.btnStopplayback.setEnabled(true);

			player = new AudioPlayer(mp3ort);
			player.start(); // new Thread

			// the timegetter sends the dmx values to the interface every
			// second
			AudioTime timegetter = new AudioTime(player, audioAna, wavearray, waveBitrate);
			timegetter.start();
		}
	}

	public void playerstop()
	{
		player.closePlayer();
		view.btnStopplayback.setEnabled(false);
		view.btnStartplayback.setEnabled(true);
		view.btnSelectmp3.setEnabled(true);
	}

}
